﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Showroom.BLL;
using System.Data;
using Wuqi.Webdiyer;

namespace ShowroomMobile
{
    public partial class Products : System.Web.UI.Page
    {
        private int CategoryID
        {
            get
            {
                return string.IsNullOrEmpty(Request["cid"]) ? 0 : Convert.ToInt32(Request["cid"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CategoryID == Convert.ToInt32(Utilities.GetKeySetting("CollectionID")))
                {
                    View_ListProducts cProds;
                    string cacheKey = "ListProducts_" + CategoryID.ToString();
                    if (Cache[cacheKey] == null)
                    {
                        cProds = View_ListProducts.GetListByCategoryID(CategoryID);
                        //Cache
                        Cache.Add(cacheKey, cProds, null, System.DateTime.Now.AddHours(Convert.ToInt32(Utilities.GetKeySetting("CacheTimeOut"))), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
                    }
                    else
                    {
                        cProds = (View_ListProducts)Cache[cacheKey];
                    }
                    Response.Redirect(string.Format("~/CollectionDetails.aspx?id={0}", cProds.ProductID));
                    return;
                }
                //add header label
                View_Category cat = new View_Category();
                cat.Where.ID.Value = CategoryID;
                cat.Query.Load();
                if (cat.RowCount > 0)
                {
                    Page.Title = lblCategoryName.Text = cat.s_Name;
                }
                else
                {
                    Response.Redirect("~/404.aspx");
                }

                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                //View_CategoryProducts cProds = new View_CategoryProducts();
                //string sFilter = string.Format("{0} = {1}", View_CategoryProducts.ColumnNames.CategoryID, CategoryID);
                //int total = cProds.Search(sFilter, "[ProductID] desc", (anpProducts.CurrentPageIndex - 1) * anpProducts.PageSize, anpProducts.PageSize);
                View_ListProducts cProds;
                string cacheKey = "ListProducts_" + CategoryID.ToString();
                if (Cache[cacheKey] == null)
                {
                    cProds = View_ListProducts.GetListByCategoryID(CategoryID);
                    //Cache
                    Cache.Add(cacheKey, cProds, null, System.DateTime.Now.AddHours(Convert.ToInt32(Utilities.GetKeySetting("CacheTimeOut"))), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
                }
                else
                {
                    cProds = (View_ListProducts)Cache[cacheKey];
                }
                cProds.Filter = string.Empty;
                cProds.Rewind();
                anpProducts.RecordCount = cProds.RowCount;

                int startIndex = (anpProducts.CurrentPageIndex - 1) * anpProducts.PageSize;
                int endIndex = startIndex + anpProducts.PageSize;
                DataTable dtSource = Utilities.GetSubDataSource(cProds.DefaultView.Table, startIndex, endIndex);

                rptListProducts.DataSource = dtSource;
                rptListProducts.DataBind();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Products.aspx.cs", "LoadData()", ex.StackTrace);
            }
        }

        protected void anpProducts_OnPageChanged(object sender, EventArgs e)
        {
            LoadData();
            ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("ChangeImageError();", 1), true);
        }
    }
}